//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    newsText:"获取中",
    title:"资讯",
    images:[],
    contentElement:[],
    count:0
  },
  _parseHtml: function(htmlSection,start)
  {
    if(this.data.count>1000)return;
    this.data.count++;
    var startE = ["<p","<h","<center"];
    var endE = ["</p>","</h","</center>"];
    var len = [4,5,9];
    var next = -1,all=startE.length,el,index=-1;
    for(var i=0;i<startE.length;i++)
    {
      var ns = htmlSection.indexOf(startE[i],start);
      if(ns==-1)
      {
        all--;
      }else if(next==-1){
          next = ns;
          index = i;
      }else
      {
        index = next < ns ? index : i;
        next = Math.min(next,ns);
      }
    }
    if(all==0)return;
    this.data.contentElement.push({txt:htmlSection.substring(start,next).replace(/&nbsp;/g,"").replace(/<(.[^>]*)>/g, "").replace("\n\n","")});
    next = htmlSection.indexOf(">",next);
    var end = htmlSection.indexOf(endE[index],next);
    //console.log(end+"  "+next+"  "+index+"  "+endE[index]);
    if(end==-1)return;
    var section = htmlSection.substring(next+1,end);
    /// 寻找图片
    var imgs = section.match(/<img(.[^>]*)>/g);
    if(imgs&&imgs.length>0)
    {
      for(var j =0;j<imgs.length;j++)
      {
        var istart = imgs[j].indexOf("http");
        var ig = imgs[j].substring(istart,imgs[j].indexOf("\"",istart));
        this.data.contentElement.push({src:ig});
      }
    }
    else
      this.data.contentElement.push({txt:section.replace(/&nbsp;/g,"").replace(/<(.[^>]*)>/g, "").replace("\n\n","")});
    this._parseHtml(htmlSection,end+len[index]);
  },
  /// 加载资源
  _loadData: function(pageIndex){
    var link = wx.getStorageSync("tmplink");
    var title = wx.getStorageSync("tmptitle");
    this.setData({title});
    wx.setNavigationBarTitle({title:title});
    /// 清除缓存
    ///wx.removeStorage("tmplink")
    wx.showLoading({"title":"获取数据中"});
    var self = this;
    wx.request({
      url:link,
      dataType:'text',
      success:function(res)
      {
        /// 先过滤一下
        var contentText = (res.data.split("artText")[1]).split("fg-line")[0].split("strategy_bottom")[0];
        var start=0,end=0;
        //console.log(contentText);
        start = contentText.indexOf(">")+1;
        end = contentText.lastIndexOf("</div>");
        contentText = contentText.substring(start,end);
        self._parseHtml(contentText,0);
        self.setData({contentElement:self.data.contentElement});
        /// 另外一种方式，废弃之
        /// 先找外层元素，一般为p或者h
        /*
        /// 查找图片
        var imgs = contentText.match(/<img(.[^>]*)>/g);
        var imgArr = [];
        if(imgs&&imgs.length>0)
          for(var i =0;i<imgs.length;i++)
          {
            var start = imgs[i].indexOf("http");
            var ig = imgs[i].substring(start,imgs[i].indexOf("\"",start));
            imgArr.push({src:ig});
          }
        if(imgArr.length>0)
        {
          self.setData({images:imgArr});
        }
        contentText = contentText.replace(/<p>/g,"\n").replace(/&nbsp;/g,"").replace(/<(.[^>]*)>/g, "");
        self.setData({newsText:contentText});
        */
      },
      fail:function()
      {
        wx.showToast({"title":"获取数据失败"});
      },
      complete:function(){
        wx.hideLoading()
      }
    });
  },
  onLoad: function () {
    var that = this
    //调用应用实例的方法获取全局数据
    this._loadData();
  }
})
